const fs = require('fs');
const path = require('path');
const JSON5 = require('json5');

// 获取传入的参数（要求release, debug或reset）
const buildMode = process.argv[2];

// 检查参数是否正确
if (!buildMode || (buildMode !== 'release' && buildMode !== 'debug')) {
    console.error('[ERROR] Usage: node script.js <release|debug>');
    process.exit(1);
}

const dependenciesName = '@rnoh/react-native-openharmony'

// 获取oh-package.json5目录的路径
const entryPackagePath = path.join(__dirname, '..', 'harmony', 'entry', 'oh-package.json5');
const harmonyPackagePath = path.join(__dirname, '..', 'harmony', 'oh-package.json5');

// 获取oh-package.json5文件的内容
const entryPackage = JSON5.parse(fs.readFileSync(entryPackagePath, 'utf-8'));
const harmonyPackage = JSON5.parse(fs.readFileSync(harmonyPackagePath, 'utf-8'));

entryPackage.dependencies[dependenciesName] = `file:../../libs/har/react_native_openharmony${buildMode === 'release' ? '_release' : ''}.har`
harmonyPackage.overrides[dependenciesName] = `../libs/har/react_native_openharmony${buildMode === 'release' ? '_release' : ''}.har`

fs.writeFileSync(entryPackagePath, JSON.stringify(entryPackage, null, 2));
fs.writeFileSync(harmonyPackagePath, JSON.stringify(harmonyPackage, null, 2));
